package cn.schoolwow.quickdao.module.operation.flow.generateEntity;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.operation.GenerateJavaSourceFileRequest;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.ArrayList;
import java.util.List;

public class SetDatabaseEntityListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        GenerateJavaSourceFileRequest generateJavaSourceFileRequest = (GenerateJavaSourceFileRequest) flowContext.checkData("generateJavaSourceFileRequest");
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);

        List<Entity> databaseEntityList = new ArrayList<>();
        if(null!=generateJavaSourceFileRequest.tableNamePredicate){
            List<String> tableNameList = quickDAOConfig.dao.getTableNameList();
            for(String tableName:tableNameList){
                if(generateJavaSourceFileRequest.tableNamePredicate.test(tableName)){
                    databaseEntityList.add(quickDAOConfig.dao.getDatabaseEntity(tableName));
                }
            }
        }else{
            databaseEntityList.addAll(quickDAOConfig.dao.getDatabaseEntityList());
        }
        flowContext.putTemporaryData("databaseEntityList", databaseEntityList);
    }

    @Override
    public String name() {
        return "设置待生成的实体类信息";
    }
}
